/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 *
 */
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import {
  AbilityType,
  APIType,
  APIVersion, CaseLevel,
  LanguageType, ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import {
  addDependency,
  addHarAbility,
  initExecuteCommand,
  runHvigorAsync,
  setDefaultStrictMode
} from '../../common/common';
import path from 'path';
import { getJson5Obj } from '../../../../src/utils/json-util';
import { expect } from '@jest/globals';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';
import { ModuleJson } from '../../../../src/options/configure/module-json-options';
import ModuleOptObj = ModuleJson.ModuleOptObj;
import AbilityObj = ModuleJson.AbilityObj;
import fse from 'fs-extra';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: {
    [ModuleTemplateType.StaticLibrary]: 1
  }
};

const testfun: TestCaseFun = async (project, taskConfig, record) => {
    const projectRootPath = project.projectRootPath();
    setDefaultStrictMode(projectRootPath);

    // 构建字节码har
    const harRootPath = project.getModule('staticLibrary').moduleRootPath();
    await addHarAbility(harRootPath, projectRootPath);
    const harCommand = ['assembleHar'];
    await runHvigorAsync(project.projectRootPath(), harCommand);
    const byteCodeHarOutputPath = path.resolve(projectRootPath, 'staticLibrary/build/default/outputs/default/staticLibrary.har');
    const projectLibsPath = path.resolve(projectRootPath, 'libs');
    fse.mkdirSync(projectLibsPath);
    fse.copyFileSync(byteCodeHarOutputPath, path.join(projectLibsPath, 'staticLibrary.har'));

    // entry添加字节码har依赖
    const entryRootPath = project.getModule('entry').moduleRootPath();
    await addDependency(entryRootPath, { 'staticLibrary': 'file:../libs/staticLibrary.har' });

    const executeCommand = initExecuteCommand({
      sync: true,
      mode: 'module',
      module: 'entry',
      tasks: 'assembleHap'
    });
    await runHvigorAsync(project.projectRootPath(), executeCommand);

  // 查看包产物module.json:build/default/intermediates/package/default/module.json
    const entryIntermediatesPkgModuleJsonPath = path.resolve(projectRootPath, 'entry/build/default/intermediates/package/default/module.json');
    const entryIntermediatesPkgModuleJsonObj: ModuleOptObj = getJson5Obj(entryIntermediatesPkgModuleJsonPath);
    entryIntermediatesPkgModuleJsonObj.module.abilities?.forEach((abilityObj: AbilityObj) => {
      if (abilityObj.name === 'harAbility') {
        // 1.hap产物module.json中包含har的ability
        expect(abilityObj.srcEntry === '@normalized:N&&&staticLibrary/src/main/ets/components/mainpage/MainPage&1.0.0').toBe(true);
      }
    });
  }
;

TestCaseBuilder.builder()
  .caseName('DTS2024102808279')
  .description('hap/hsp依赖字节码har，hap/hsp包的module.json中srcEntry值为空')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.AR).build())
  .testFun(testfun)
  .load();
